package com.spark.lola.controller;


import com.spark.lola.mapper.StoreProductMapper;
import com.spark.lola.service.IStoreProductService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.entity.warehouse.StoreProduct;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengyejun
 * @since 2020-06-10
 */
@RestController
@RequestMapping("/api/store-product")
public class StoreProductController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(StoreProductController.class);
    @Autowired
    private StoreProductMapper currentMapper;
    @Autowired
    private IStoreProductService currentService;

    @RequiresPermissions(value = "m7130")
    @RequestMapping(value = "/findBy", consumes = "application/json")
    public ResponseBean findBy(@RequestBody QueryParam<StoreProduct> queryBean,
                               @SessionAttribute ShiroUser shiroUser) {
        PageParam<StoreProduct> pageParam = queryBean.toPageParam();
        IPage<StoreProduct> page = currentMapper.selectPage(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m7134")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable Long id,
                                 @SessionAttribute ShiroUser shiroUser) {
        StoreProduct model;
        if (id == 0) {
            model = new StoreProduct();
        } else {
            model = currentService.getById(id);
        }
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = {"m7131", "m7133"}, logical = Logical.OR)
    @RequestMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody StoreProduct form, @SessionAttribute ShiroUser shiroUser) throws Exception {
        StoreProduct model = new StoreProduct();
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = currentService.getById(form.getId());
        }
        DataBeanUtils.buildUpdateBean(model, form);
        currentService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = "m7135")
    @RequestMapping("/on")
    public ResponseBean on(@RequestParam String nos) {
        int count = currentService.updateUsedByIds(CommonConstant.Y, nos);
        return ResponseBean.success(null, count);
    }

    @RequiresPermissions(value = "m7136")
    @RequestMapping("/un")
    public ResponseBean un(@RequestParam String nos) {
        int count = currentService.updateUsedByIds(CommonConstant.N, nos);
        return ResponseBean.success(null, count);
    }

    @RequiresPermissions(value = "m7132")
    @RequestMapping("/del")
    public ResponseBean del(@RequestParam(value = "nos") List<Long> ids) {
        ids.forEach(id -> {
            currentService.removeById(id);
        });
        return ResponseBean.success();
    }
}
